<include file="Public:header" />
<style>
	.viewEditBox8749{display:none;}
	input,textarea{border:1px solid #ccc; border-radius:2px; box-shadow:0 1px 5px #ccc inset; text-indent:0.5em;}
	input{height:35px; line-height:1;}
	body{background:#f5f5f5;}
	.list-group {
		margin-bottom: 5px;
	}
	.pages {
		text-align: center;
	}
	a{color:#333;}
	.panel{background:#fff;}
	.headernav a{font-size:1.5em; color:#000;}
	.media{padding-left:5px; padding-bottom:10px;}
</style>
<script src="__PUBLIC__/Js/validform/Validform.js"></script>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/Js/validform/style.css">
<script>
	$.holdReady(false);
	$(document).ready(function(e) {
		$.ajax({
			url:"{:U('Ajax/getRegion')}",
			data:"",
			type:"POST",
			dataType:"html",
			success: function(data){
				$('#areabox').html(data);
			}
		});
	});
	function removnextAll(e){
		if($(e).next().length>0){
			removnextAll($(e).next());
			$(e).next().remove();
		}
	}
	function sendRegion(e){
		if(e.value>0){
			$.ajax({
				url:"{:U('Ajax/getRegion')}",
				data:"fid="+e.value,
				type:"POST",
				dataType:"html",
				success: function(data){
					removnextAll(e);
					$(e).parent().append(data);
				}
			});
		}else{
			removnextAll(e);
		}
		return false;
	}
</script>
<body>
<div style="background:#fff; height:40px; line-height:40px; border-bottom:1px solid #ccc;" class="headernav">
	<a href="javascript:history.go(-1);" style="height:40px; width:40px; display:block; float:left; text-align:center;"><i class="fa fa-angle-left"></i></a>
	<a href="__APP__/" style="height:40px; width:40px; display:block; float:right; text-align:center;"><i class="fa fa-home"></i></a>
	<a href="{:U('Cart/index')}" style="height:40px; width:40px; display:block; float:right; text-align:center;"><i class="fa fa-shopping-cart"></i></a>
</div>
<form class="checkoutform" id="checkout" action="<?php echo U('createOrder1');?>" method="post">
	<div class="main">
		<!-- 免运费 -->
		<eq name="noshipping" value="0">
			<div class="panel panel-default" style="border-radius:0; margin-bottom:5px; margin-top:5px;">
				<div class="panel-body">
					<a href="javascript:void(0);" onClick="editAddress();">
						<div class="row" style="padding:8px 0;">
							<div class="col-xs-2"><i class="fa fa-truck" style="font-size:2.5em;"></i></div>
							<div class="col-xs-8">
								<span style="color:red; font-size:14px;" >
								<empty name="address">
									<span id="getaddress" style="color:#000000; font-size:13px; "><?php if(!is_weixin()){?><a href="<?php echo U('address');?>">点击这里填写地址:</a><?php }?></span>
									<else />
								<span id="getaddress" style="color:#000000; font-size:13px; ">{$yourname}&nbsp;&nbsp;{$tel}<br>
					{$address}</span>
								</empty>
								</span>
								<input style="display:none;" name="yourname" datatype="*" type="text" id="yourname" nullmsg="收货人姓名不能为空" error="收货人姓名不能为空" value="{$yourname}">
								<input style="display:none;" name="address" datatype="*" type="text" id="address" nullmsg="收货地址不能为空" error="收货地址不能为空" value="{$address}">
								<input style="display:none;" name="tel" datatype="*" type="text" id="tel" nullmsg="电话不能为空" error="电话不能为空" value="{$tel}">
								<input type="hidden" value="{$regionids}" sendName="regionids" id="regionids" name="regionids" class="form-control sendListInput">
								<input type="hidden" value="" sendName="nationalCode" id="nationalCode" name="nationalCode" class="form-control sendListInput">
							</div>
							<div class="col-xs-2"></div>
						</div>
				</div></a>
			</div>
			<div style="margin:0 5px;"><a href="{:U('showAddress')}" class="btn btn-success" style="display:block; margin:5px;">点击填写配送地址</a>
			</div>
			<notempty name="addressSign_info">
				<script type="text/javascript">
					//获取共享地址
					function editAddress()
					{
						WeixinJSBridge.invoke(
								'editAddress',
								{$addressSign_info},
								function(res){
									//alert(res.err_msg);
									if(res.err_msg == "edit_address:ok"){
										//地区代码
										var nationalCode = res.nationalCode;
										var value1 = res.proviceFirstStageName;
										var value2 = res.addressCitySecondStageName;
										var value3 = res.addressCountiesThirdStageName;
										var value4 = res.addressDetailInfo;
										var address = value1+value2+value3+value4
										var tel = res.telNumber;
										var username = res.userName;
										//alert(value1 + value2 + value3 + value4 + ":" + tel+":"+username);
										var getaddress = ""+username+"&nbsp;&nbsp;&nbsp;&nbsp;"+tel+" <br/> "+""+address;
										//alert(username == undefined);
										if(username == undefined || tel == undefined || address == NaN){
											username = '';
											tel = '';
											address = '';
											getaddress = '点击获取地址';
										}
										$("#getaddress").html(getaddress);
										document.getElementById("yourname").value = username ;
										document.getElementById("address").value= address ;
										document.getElementById("tel").value= tel ;
										document.getElementById('nationalCode').value = nationalCode;
										//计算运费
										$.post('{:U('echoShippingFeell')}', sendData(), function(data){
											$('#_total').html(data.total);
											delete(data.total);
											$('input[name="shipping_fee"]').val(data.fee);//总运费
											delete(data.fee);
											for(var name in data){
												$(name).html(data[name]);
												$(name).val(data[name]);
											}
										},'json');
									}else{
										//alert(res.err_msg);
										//alert('获取微信地址失败，请手动填写地址');
									}
								}
						);
					}
					window.onload = function(){
						if (typeof WeixinJSBridge == "undefined"){
							if( document.addEventListener ){
								document.addEventListener('WeixinJSBridgeReady', editAddress, false);
							}else if (document.attachEvent){
								document.attachEvent('WeixinJSBridgeReady', editAddress);
								document.attachEvent('onWeixinJSBridgeReady', editAddress);
							}
						}else{
							editAddress();
						}
					};
				</script>
			</notempty>
		</eq>
	<!-- 发票管理 -->
	<notempty name="InvoiceList">
		<input type="hidden" sendName="invoice_id" name="invoice_id" class="sendListInput" id="invoice_id" value="">
		<!-- Single button -->
		<div class="btn-group" style="padding:5px 10px; width:100%; background:#fff;">
			<button type="button" style="width:100%;" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
				<span id="invoicename">选择发票类型</span> <span class="caret"></span>
			</button>
			<ul class="dropdown-menu" style="width:100%; min-width:100%;">
				<li><a href="javascript:void(0);" onClick="setInvoice_id(0,'不需要发票');">不需要发票</a></li>
				<volist name="InvoiceList" id="vo">
					<li><a href="javascript:void(0);" onClick="setInvoice_id({$vo.id},'{$vo.title}');">{$vo.title}</a></li>
				</volist>
			</ul>
		</div>
		<div class="btn-group" id="taxtitle" style="padding:5px 10px; width:100%; display:none; background:#fff;"><input class="form-control" value="" name="taxtitle" type="text" placeholder="请填写抬头" errormsg="您选择开发票，请填写抬头！" nullmsg="您选择开发票，请填写抬头！"></div>
		<script>
			function setInvoice_id(id, title){
				$('#invoice_id').val(id);
				if(id > 0){
					$('#taxtitle').find('input').attr('datatype', '*');
					$('#taxtitle').show();
					$(".checkoutform").Validform({
						tiptype:1
					});
				}else{
					$('#taxtitle').hide();
					$('#taxtitle').find('input').removeAttr('datatype');
					$(".checkoutform").Validform({
						tiptype:1
					});
				}
				$.post('{:U('echoShippingFee')}', sendData(), function(data){
					//$('#Shippingfee').html(data.shipping_fee);
					$('#invoice_id').val(id);
					$('#invoicename').html('发票类型：'+title+' ');
					$('#_total').html(data.total);
				},'json');
			}
		</script>
	</notempty>
	<!-- 发票管理 -->
	<!-- 积分抵现 -->
	<div style="margin:5px;"><if condition="is_array($integral_pay) and $integral gt 0">
		<div class="alert alert-warning" role="alert" style="margin-bottom:5px; padding:5px;">
			<strong>温馨提示:</strong>您有{$integral}积分可用于抵现。当前订单允许使用{$integral_pay.integralsum}积分。
		</div>
		<input onBlur="integralPay(this);" type="number" placeholder="请输入积分" sendName="integral" id="integral" name="integral_pay" class="form-control sendListInput">
		<script>
			function integralPay(e){
				$.post('{:U('echointegralPay')}',sendData(), function(data){
					if(data.status == 1){
						$('#_total').html('¥'+data.total);

					}else{
						error(data.info);
					}
				}, 'json');
			}
		</script>
	</if></div>
	<!-- 积分抵现结束 -->
	<!-- 优惠券列表AliceXT -->
	<notempty name="coupons">
		<input type="hidden" sendName="coupon_sn_id" name="coupon_sn_id" class="sendListInput" id="coupon_sn_id">
		<!-- Single button -->
		<div class="btn-group" style="padding:5px 10px; width:100%; background:#fff;">
			<button type="button" style="width:100%;" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
				<span id="coupon_title">请选择优惠券</span> <span class="caret"></span>
			</button>
			<ul class="dropdown-menu" style="width:100%; min-width:100%;">
				<li><a href="javascript:void(0);" onClick="setcoupon_sn_id('',this);">不使用优惠券</a></li>
				<volist name="coupons" id="sn">
					<li><a href="javascript:void(0);" onClick="setcoupon_sn_id('{$sn.sn}',this);">{$sn.title}|优惠金额：{$sn.discount}</a></li>
				</volist>
			</ul>
		</div>
		<script type="text/javascript">
			function setcoupon_sn_id(id,e){
				$('#coupon_title').text($(e).text());
				$('#coupon_sn_id').val(id);
				$.post('{:U('echoCoupon')}', sendData(), function(data){
					$('#_total').html(data.total);
				},'json');
			}
		</script>
	</notempty>
	<!-- 优惠券列表AliceXT -->
	<div style="margin:0 5px;">
		<input  type="text"  name="Guestbook" class="form-control" placeholder="给我留言">
	</div>
	<volist name="productList" id="vo">
		<?php
			if($vo[0]['supplier_id']){
				$supplier_name=D('Supplier')->where('id = '.$vo[0]['supplier_id'])->getfield('susername');
			}else{
				$supplier_name = '无供货商';
			}
		?>
		<div class="panel panel-info" style=" margin: 5px 5px 0px">
			<div class="panel-heading"><i class="glyphicon glyphicon-list"></i> 供货商：{$supplier_name}</div>

			<?php $mykey==0;$sup_price=0; foreach($vo as $k => $v){ ?>
				<?php if($mykey > 0){?>
					<div class="line_" style="height:1px; margin:0 5px; background:#ddd;"></div>
				<?php }?>
				<div class="media" style="position:relative;" id="cart_<?php echo $v['id'];?>">
					<div class="media-left">
						<a href="##" style="width:80px; height:80px; display:block;">
							<img width="80" height="80" class="media-object" src="<?php echo getThumbPath($v['img_url']);?>" data-holder-rendered="true" style="width: 80px; height: 80px;">
						</a>
					</div>
					<div class="media-body">
						<h4 class="media-heading" style="font-weight:normal; color:#999;"><?php echo $v['title'];?></h4>
						<p>
							<?php if(!empty($v['attrmes'][0])){?>
							<span style="color:rgba(203,189,190,1)">规格:&nbsp;&nbsp;</span>
							<?php foreach($v['attrmes'] as $vos){?>
							<span><?php echo $vos ?>&nbsp;&nbsp;</span>
							<?php }?>
							<?php }?></p>
						<div class="row" style="line-height:30px;">
							<div class="col-xs-12"><span style="color:#f00; font-size:1.2em; font-weight:bold; ">¥<?php echo $v['price'];?></span>&nbsp;&times;&nbsp;<?php echo $v['num'];?></div>
						</div>
					</div>
				</div>
				<?php $sup_price+=($v['price']*$v['num']); ?>
			<?php $mykey++;} ?>
			<input type="hidden" name="pre[]" value="<?php echo ','.$vo[0]['supplier_id'].','.$sup_price; ?>">
		</div>
		<!-- 配送费用 -->
		<div class="shipping_set" style="margin:10px; text-align:right; margin-right:20px;">
			<?php
				$sendtype=D('Shipping')->where('status = 1 and member_id = 0 ')->order('sort')->getfield('id');
				if($vo[0]['shipping_id']){
					$sendtype=$vo[0]['shipping_id'];
				}elseif($vo[0]['supplier_id']){
					$ship_member=D('Supplier')->where('id = '.$vo[0]['supplier_id'])->getfield('member_id');
					if($ship_member){
						if(D('Shipping')->where('member_id = '.$ship_member)->count()){
							$sendtype=D('Shipping')->where('status = 1 and member_id = '.$ship_member)->order('sort')->getfield('id');
						}
					}
				}
				$sendname=D('Shipping')->where('id = '.$sendtype)->getfield('title');
			?>
			<input type="hidden" sendName="shipping_id[<?php echo $vo[0]['supplier_id']; ?>]" name="shipping_id_<?php echo $vo[0]['supplier_id'];?>" class="sendListInput" id="shipping_id_<?php echo $vo[0]['supplier_id'];?>" value="<?php echo $sendtype; ?>" nullmsg="请选择配送方式" datatype="*" errormsg="请选择配送方式">
			<input type="hidden" name="fee[]" value="" id="fee_{$vo[0]['supplier_id']}">
			<input type="hidden" name="shipping[]" value="<?php echo ','.$vo[0]['supplier_id'].','.$sendtype;?>" id="shipping_{$vo[0]['supplier_id']}">
			配送方式：{$sendname|default='无配送方式'} 配送费用：<span style="color:#f00; font-size:1.3em; font-weight:bold; font-family:Arial;" id="Shippingfee_{$vo[0]['supplier_id']}">0</span>
		</div>
		<!-- 配送费用结束 -->
	</volist>
	<!-- 发票费用 -->
	<!-- 发票费用结束 -->
	<div style="height:80px;"></div>
	<div style="position:fixed; bottom:0px; width:100%; background:rgba(219,214,214,1.00); z-index:10000; height:50px; line-height:50px;">
		<div class="row">
			<div class="col-sm-7 col-xs-7" style="text-align:left; padding:0; padding-left:8px; line-height:50px;">合计：<span style="font-family:Arial;color:red; font-size:1.5em; font-weight:bold;"><span id="_total" total="¥<?php echo $total;?>">¥<?php echo $total;?></span></span></div>
			<div class="col-xs-5" style="padding:0;"><button id="gotopay" disabled type="submit" class="btn-danger" style="display:block; width:100%; text-align:center; font-size:1.3em;" ><i class="glyphicon glyphicon-credit-card"></i> 立即支付</button></div>
		</div>
	</div>
	</div>
	<input type="hidden" name="shipping_fee" >
</form>
<style>
	table{margin-top:10px !important;}
	td,th{padding:5px 12px !important;}
	#areabox select{margin:5px;}
</style>
<script type="text/javascript">
    function sendData(){
        console.log(1);
        var sendListInput = new Array();
        $('.sendListInput').each(function(index, element) {
            sendListInput.push('"' +$(this).attr('sendName') + '":"' + $(this).val() + '"');
        });
        console.log(sendListInput);
        return eval('({' + sendListInput.toString() + '})');
    }
	function setShipping_id(supplier_id,id, title){
		$('#shipping_id_'+supplier_id).val(id);
		$.post('{:U('echoShippingFeell')}', sendData(), function(data){
            $('#_total').html(data.total);
			delete(data.total);
			$('input[name="shipping_fee"]').val(data.fee);//总运费
			delete(data.fee);
			for(var name in data){
				$(name).html(data[name]);
				$(name).val(data[name]);
			}
		},'json');
	}
	$(function(){
		//$(".registerform").Validform();  //就这一行代码！;
		$('#gotopay').removeAttr("disabled");
		$(".checkoutform").Validform({
			tiptype:1
		});
	})
	//默认选中第一个物流方式
	$('.shipping_set').each(function(){
		var	thisSupplierId=$(this).children('li').eq(0).attr('supplier_id');
		var	thisId=$(this).children('li').eq(0).attr('id');
		var	thisTitle=$(this).children('li').eq(0).attr('title');
		setShipping_id(thisSupplierId,thisId,thisTitle);
	});
</script>
<include file="Public:footer" />
